import React from 'react';
import { bindActionCreators } from '@/redux';
import store from '@/store';
import * as actions from '@/store/actions/counter'



const obj = bindActionCreators(actions, store.dispatch);


export default class Counter extends React.Component {
    constructor(props) {
        super(props);
        const state = store.getState().counter;
        this.state = {
            count: state.count
        }
    }

    componentDidMount() {
        // 订阅 当状态改变时，触发更新
        this.unsubscribe = store.subscribe(() => {
            this.setState({
                count: store.getState().counter.count
            })
        })
    }
    componentWillunmount() {
        // 取消订阅
        this.unsubscribe();
    }

    render() {
        return <>
            <div>{this.state.count}</div>
            <button onClick={() => obj.add('1', '2')}>+</button>
            <button onClick={obj.minus}>-</button>
        </>
    }
}